using System;
using System.Threading.Tasks;
using JlPay.Sdk.Core;
using JlPay.Sdk.Services.Ledger.Merchconf;
using JlPay.Sdk.Models.Ledger.Merchconf;

namespace JlPay.Sdk.Examples.Ledger.Merchconf.MerchIssueQuery
{
    /// <summary>
    /// 商户分账发行查询独立示例程序
    /// </summary>
    public class MerchIssueQueryDemo
    {
        /// <summary>
        /// 主程序入口
        /// </summary>
        /// <param name="args">命令行参数</param>
        /// <returns>任务</returns>
        public static async Task Main(string[] args)
        {
            Console.WriteLine("==========================================");
            Console.WriteLine("===== 商户分账发行查询独立示例程序 =====");
            Console.WriteLine("==========================================");
            Console.WriteLine();

            await MerchIssueQueryDemoAsync();

            Console.WriteLine("==========================================");
            Console.WriteLine("===== 示例运行完毕 =====");
            Console.WriteLine("==========================================");
        }

        /// <summary>
        /// 创建配置对象
        /// </summary>
        /// <returns>配置对象</returns>
        private static Config CreateConfig()
        {
            return DemoConfig.CreateConfigWithCrypto();
        }

        /// <summary>
        /// 构建商户分账发行查询请求
        /// </summary>
        /// <returns>请求对象</returns>
        private static MerchIssueQueryRequest BuildMerchIssueQueryRequest()
        {
            var request = new MerchIssueQueryRequest();
            
            // 设置请求参数
            request.OrgCode = "50720711";
            request.MchId = "849581183510017";
            
            return request;
        }

        /// <summary>
        /// 商户分账发行查询示例
        /// </summary>
        public static async Task MerchIssueQueryDemoAsync()
        {
            Console.WriteLine("开始执行商户分账发行查询示例");
            
            try
            {
                // 创建配置对象和商户配置服务
                var config = CreateConfig();
                var merchconfService = new MerchconfService(config);
                
                // 创建商户分账发行查询请求
                var request = BuildMerchIssueQueryRequest();
                
                // 发送请求
                var response = await merchconfService.QueryMerchIssueAsync(request);
                
                // 处理响应结果
                response.HandleResponse("商户分账发行查询", config);
            }
            catch (Exception ex)
            {
                var config = CreateConfig();
                var response = new MerchIssueQueryResponse();
                response.HandleResponseWithException("商户分账发行查询", config, ex);
            }
            
            Console.WriteLine("商户分账发行查询示例执行完毕");
        }
    }
} 